package com.teachoa.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class PageTag extends TagSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -4564861302612155867L;
	
	/**
	 * 
		<ul class="pagination">
	        <li><a href="javascript:;" onclick="turnpage(1)">上一页</a></li>
	        <li class="active">
	            <a href="#">1</a>
	        </li>
	        <li><a href="#">2</a></li>
	        <li><a href="#">3</a></li>
	        <li><a href="#">4</a></li>
	        <li><a href="#">下一页</a></li>
	    </ul>
	 * 
	 */
	
	/**
	 * {pageNo:xxx,total:xxx,jsName:xxx}
	 * 
	 */
	
	
	/**
	 * 1 2345
	 * 12 345
	 * 123 45
	 * 1234 5
	 * 456 78
	 * 567 89
	 * ....
	 * 10 11 12 13 14
	 * 11 12 13 14 15
	 */
	
	/*private void test(int pageNo, int total) {
		//int totalPage = 19;
		if(pageNo<=5) System.out.println("1,2,3,4,5");
		if(pageNo>5&&pageNo<total-5) System.out.println((pageNo-2)+","+(pageNo-1)+","+pageNo+","+(pageNo+1)+","+(pageNo+2));
		if(pageNo>=total-5) System.out.println((total-4)+","+(total-3)+","+(total-2)+","+(total-1)+","+total);
	}*/
	
	/*public static void main(String[] args) {
		new PageTag().test(6, 9);
	}*/
	
	/**
	 * 
	 *  <div class="dataTables_paginate paging_full_numbers" id="sorting-advanced_paginate">
			<a tabindex="0" class="first paginate_button paginate_button_disabled" id="sorting-advanced_first">首页</a>
			<a tabindex="0" class="previous paginate_button paginate_button_disabled" id="sorting-advanced_previous">上一页</a>
			<span>
				<a tabindex="0" class="paginate_active">1</a>
				<a tabindex="0" class="paginate_button">2</a>
				<a tabindex="0" class="paginate_button">3</a>
				<a tabindex="0" class="paginate_button">4</a>
				<a tabindex="0" class="paginate_button">5</a>
				<a tabindex="0" class="paginate_button">6</a>
			</span>
			<a tabindex="0" class="next paginate_button" id="sorting-advanced_next">下一页</a>
			<a tabindex="0" class="last paginate_button" id="sorting-advanced_last">末页</a>
		</div>
	 * 
	 */
	
	private int pageNumber;
	private int totalRow;
	private int totalPage;
	
	private void init() {
//		pageNumber = 3;
//		totalRow = 100;
//		totalPage = 10;
		System.err.println("pageNumber="+pageNumber);
		System.err.println("totalRow="+totalRow);
		System.err.println("totalPage="+totalPage);
	}
	
	private String getPageHtml() {
		StringBuilder sb = new StringBuilder();
		sb.append("<div class='dataTables_paginate paging_full_numbers' id='sorting-advanced_paginate'>")
		.append(createOuter("首页"));
		if(pageNumber!=1) sb.append(createOuter("上一页"));
		sb.append("<span>");
		if(pageNumber<=5&&totalPage>=5) sb.append(createInner(1, 6));
		else if(pageNumber<=5&&totalPage<=5) sb.append(createInner(1, totalPage+1));
		else if(pageNumber>5&&pageNumber<totalPage-5) sb.append(createInner(pageNumber-2, totalPage+3));
		else if(pageNumber>=totalPage-5) sb.append(createInner(totalPage-4, totalPage+1));
		sb.append("</span>");
		if(pageNumber!=totalPage) sb.append(createOuter("下一页"));
		sb.append(createOuter("末页")).append("</div>");
		return sb.toString();
	}
	
	private String createOuter(String desc) {
		int page = 0;
		String clazz = null;
		if(desc.equals("首页")) {
			page = 1;
			clazz = "first";
		} else if(desc.equals("上一页")) {
			page = pageNumber==1?1:pageNumber-1;
			clazz = "previous";
		} else if(desc.equals("下一页")) {
			page = pageNumber==totalPage?totalPage:pageNumber+1;
			clazz = "next";
		} else if(desc.equals("末页")) {
			page = totalPage;
			clazz = "last";
		}
		String html = "<a tabindex='0' class='"+clazz+" paginate_button'"
				+ " onclick='turnpage(\""+page+"\")'>"+desc+"</a>";
		return html;
	}
	
	private String createInner(int start, int end) {
		StringBuilder sb = new StringBuilder();
		for(int i=start;i<end;i++) {
			String clazz = i==pageNumber?"active":"button";
			String html = "<a tabindex='0' class='paginate_"+clazz+"' onclick='turnpage(\""+i+"\")'>"+i+"</a>";
			sb.append(html);
		}
		return sb.toString();
	}

	@Override
	public int doStartTag() throws JspException {
		try {
			init();
			pageContext.getOut().write(getPageHtml());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public static void main(String[] args) {
		PageTag pt = new PageTag();
		//pt.setMap(Maps.newHashMap("pageNo", 3, "total", 15, "js", "page"));
		pt.init();
		System.out.println(pt.getPageHtml());
	}

}
